home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / NetWare Development / NLM Client Example / LServerActions.cp < prev    next >
Encoding:
Text File  |  1994-10-06  |  8.5 KB  |  342 lines  |  [TEXT/MPCC]

  1. // ===========================================================================
  2. //    LServerActions.cp            The Server Actions window
  3. // ===========================================================================
  4. //
  5. //    This program is NOT necessarily the model of good programming.  The attention
  6. //    has been spent on the server side.  However, if you wish to use this
  7. //    as a starter program, feel free.
  8. //
  9. //    This program was written with Metrowerks C/C++ for 68k, using the
  10. //    PowerPlant class hierarchy.
  11. //
  12. //    It was written by Nick Brosnahan and Jamie Osborne. 9/94
  13. //
  14. // Apple Computer, Inc. makes no warranties about the the fitness of this code
  15. // for any purpose and will not be held liable for any damages you may suffer
  16. // from using this code.  Furthermore, Apple Computer, Inc. will not be held
  17. // liable for the death of Elvis or any other space alien.
  18. //
  19. // Your mileage may vary.
  20.  
  21. #include "LServerActions.h"
  22. #include "NetLister.h"
  23. #include <LStdControl.h>
  24. #include <LCaption.h>
  25. #include <LEditField.h>
  26. #include <PP_Messages.h>
  27.  
  28. #ifndef __TYPES__
  29. #include <Types.h>
  30. #endif
  31.  
  32. #ifndef __PACKAGES__
  33. #include <Packages.h>
  34. #endif
  35.  
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38.  
  39. // additions
  40. typedef AddrBlock ATAddress;
  41.  
  42. #include <string.h>
  43.  
  44. const MessageT    cmd_DoGetName = 1011;
  45. const MessageT    cmd_DoCloseConn = 1012;
  46. const MessageT    cmd_DoCurrConn = 1013;
  47. const MessageT    cmd_DoPeakConn = 1014;
  48.  
  49. enum {kRequestOpen = 1, kRequestName, kRequestCurCon, kRequestPeakCon, kRequestQuit };
  50.  
  51. const int kRespBufferSize = 256;
  52.  
  53. // ---------------------------------------------------------------------------
  54. //        • CreateAppWindowStream [static]
  55. // ---------------------------------------------------------------------------
  56. //    Return a new AppWindow object initialized using data from a Stream
  57.  
  58. LServerActions*
  59. LServerActions::CreateServerActionsStream(
  60.     LStream    *inStream)
  61. {
  62.     return (new LServerActions(inStream));
  63. }
  64.  
  65.  
  66. // ---------------------------------------------------------------------------
  67. //        • LServerActions
  68. // ---------------------------------------------------------------------------
  69. //    Default Constructor
  70.  
  71. LServerActions::LServerActions()
  72. {
  73.     mSocket = -1;
  74.     mGetName = mCloseConn = mCurrConn = mPeakConn = NULL;
  75.     mResult1 = NULL;
  76.     mIsConnected = FALSE;
  77.     mRespBuffer = NULL;
  78. }
  79.  
  80.  
  81. // ---------------------------------------------------------------------------
  82. //        • LServerActions(SWindowInfo&)
  83. // ---------------------------------------------------------------------------
  84. //    Construct AppWindow from the data in a struct
  85.  
  86. LServerActions::LServerActions(
  87.     SWindowInfo    &inWindowInfo)
  88.         : LWindow(inWindowInfo)
  89. {
  90.     mSocket = -1;
  91.     mGetName = mCloseConn = mCurrConn = mPeakConn = NULL;
  92.     mResult1 = NULL;
  93.     mIsConnected = FALSE;
  94.     mRespBuffer = NULL;
  95. }
  96.  
  97.  
  98. // ---------------------------------------------------------------------------
  99. //        • LServerActions(ResIDT, Uint32, LCommander*)
  100. // ---------------------------------------------------------------------------
  101. //    Construct a AppWindow from a WIND Resource with the specified attributes
  102. //    and SuperCommander
  103. //
  104. //    Side Effect: Created window becomes the current port
  105.  
  106. LServerActions::LServerActions(
  107.     ResIDT        inWINDid,
  108.     Uint32        inAttributes,
  109.     LCommander    *inSuper)
  110.         : LWindow(inWINDid, inAttributes, inSuper)
  111. {
  112.     mSocket = -1;
  113.     mGetName = mCloseConn = mCurrConn = mPeakConn = NULL;
  114.     mResult1 = NULL;
  115.     mIsConnected = FALSE;
  116.     mRespBuffer = NULL;
  117. }
  118.  
  119.  
  120. // ---------------------------------------------------------------------------
  121. //        • LServerActions(LStream*)
  122. // ---------------------------------------------------------------------------
  123. //    Construct a AppWindow from the data in a Stream
  124.  
  125. LServerActions::LServerActions(
  126.     LStream    *inStream)
  127.         : LWindow(inStream)
  128. {
  129.     mSocket = -1;
  130.     mGetName = mCloseConn = mCurrConn = mPeakConn = NULL;
  131.     mResult1 = NULL;
  132.     mIsConnected = FALSE;
  133.     mRespBuffer = NULL;
  134. }
  135.  
  136.  
  137. // ---------------------------------------------------------------------------
  138. //        • ~LServerActions
  139. // ---------------------------------------------------------------------------
  140. //    Destructor
  141.  
  142. LServerActions::~LServerActions()
  143. {
  144.     if (mRespBuffer)
  145.         free (mRespBuffer);
  146. }
  147.  
  148.  
  149. void LServerActions::AttemptClose()
  150. {
  151.     // Try to close the connection, but don't bother checking for the error code 
  152.     DoRequest(mSocket, &mServerAddress, kRequestQuit, mRespBuffer, kRespBufferSize);
  153.     LWindow::AttemptClose();
  154. }
  155.  
  156. void LServerActions::ListenToMessage(
  157.     MessageT    inMessage,
  158.     void        *ioParam)
  159. {    
  160.     int stat;
  161.  
  162.     switch (inMessage)
  163.     {
  164.         case cmd_DoGetName:
  165.             stat = DoRequest(mSocket, &mServerAddress, kRequestName, mRespBuffer, kRespBufferSize);
  166.             c2pstr(mRespBuffer);
  167.             mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
  168.             mResult1->Refresh();
  169.         break;
  170.         
  171.         case cmd_DoCloseConn:
  172.             stat = DoRequest(mSocket, &mServerAddress, kRequestQuit, mRespBuffer, kRespBufferSize);
  173.             c2pstr(mRespBuffer);
  174.             mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
  175.             mResult1->Refresh();
  176.             if (stat != -1)
  177.                 SetConnected(FALSE);
  178.             break;
  179.         case cmd_DoCurrConn:
  180.             stat = DoRequest(mSocket, &mServerAddress, kRequestCurCon, mRespBuffer, kRespBufferSize);
  181.             c2pstr(mRespBuffer);
  182.             mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
  183.             mResult1->Refresh();
  184.             break;
  185.         case cmd_DoPeakConn:
  186.             stat = DoRequest(mSocket, &mServerAddress, kRequestPeakCon, mRespBuffer, kRespBufferSize);
  187.             c2pstr(mRespBuffer);
  188.             mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
  189.             mResult1->Refresh();
  190.             break;
  191.         default:
  192.             if (GetSuperCommander() != nil) 
  193.                 GetSuperCommander()->ProcessCommand(inMessage, ioParam);
  194.             break;
  195.     }
  196. }
  197.  
  198. void LServerActions::DoSetupServerActions(ATAddress addr)
  199. {
  200.     mServerAddress = addr;
  201.     mGetName = (LStdButton *)this->FindPaneByID('act1');
  202.     mCloseConn = (LStdButton *)this->FindPaneByID('act2');
  203.     mCurrConn = (LStdButton *)this->FindPaneByID('act3');
  204.     mPeakConn = (LStdButton *)this->FindPaneByID('act4');
  205.                 
  206.     mGetName->AddListener(this);
  207.     mCloseConn->AddListener(this);
  208.     mCurrConn->AddListener(this);
  209.     mPeakConn->AddListener(this);
  210.  
  211.     mResult1 = (LCaption *)this->FindPaneByID('rlt1');
  212.  
  213.     mRespBuffer = (char *) malloc(kRespBufferSize);
  214.     mSocket = GetSocket();
  215.     
  216.     return;
  217. }
  218.  
  219. void LServerActions::DoClient()
  220. {
  221.     int result;
  222.  
  223.     result = DoRequest(mSocket, &mServerAddress, kRequestOpen, mRespBuffer, kRespBufferSize);
  224.     c2pstr(mRespBuffer);
  225.     mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
  226.     
  227.     if (result != -1)
  228.     {
  229.         mServerAddress.aSocket = result;
  230.         SetConnected(TRUE);
  231.     }
  232. }
  233.  
  234. unsigned int GetSocket()
  235. {
  236.     ATPParamBlock pb;
  237.     int stat;
  238.  
  239.     memset(&pb, 0, sizeof(pb));
  240.     stat = POpenATPSkt(&pb, false);
  241.     Assert_(!stat);
  242.     return pb.ATPatpSocket;
  243. }
  244.  
  245. int DoRequest(unsigned socket, ATAddress *addr, int request, void *buffer, int bufferLen)
  246. {
  247.     char    requestBuff[64];
  248.     int        len;
  249.     
  250.     ATPParamBlock pb;
  251.     BDSType bds;
  252.     int numResps;
  253.     OSErr stat;
  254.  
  255.     if (request == kRequestOpen)
  256.         strcpy(requestBuff, "Hello Server, from the Client.");
  257.     else
  258.         strcpy(requestBuff, "Here is a request.");
  259.  
  260.     len = strlen(requestBuff) + 1;
  261.         
  262.     numResps = BuildBDS((Ptr) buffer, (Ptr)&bds, (short) bufferLen);
  263.  
  264.     memset(&pb, 0, sizeof(pb));
  265.     pb.ATPuserData = (long) request;
  266.     pb.ATPatpSocket = socket;
  267.     pb.ATPatpFlags = atpXOvalue;
  268.     pb.ATPaddrBlock = *addr;
  269.     pb.ATPreqLength = len;
  270.     pb.ATPreqPointer = (Ptr)requestBuff;
  271.     pb.ATPbdsPointer = (Ptr)&bds;
  272.     pb.ATPnumOfBuffs = numResps == 1;
  273.     pb.ATPtimeOutVal = 6;
  274.     pb.ATPretryCount = 2; 
  275.  
  276.     stat = PNSendRequest(&pb, false);
  277.  
  278.     if (stat != 0)
  279.         sprintf((char *)buffer, "Received error from PNSendRequest: %d", (int)stat);
  280.  
  281.     return bds[0].userBytes;
  282.  
  283. }
  284.  
  285. int SendATRequest(unsigned socket, ATAddress *dest, void *reqBuff, 
  286.         int reqSize, void *respBuff, int respBuffLen, int whichRequest)
  287. {
  288. ATPParamBlock pb;
  289. BDSType bds;
  290. int numResps;
  291. OSErr stat;
  292. short myNode, myNet;
  293.  
  294.     stat = GetNodeAddress(&myNode,&myNet);
  295.     
  296.     numResps = BuildBDS((Ptr) respBuff, (Ptr)&bds, (short) respBuffLen);
  297.  
  298.     memset(&pb, 0, sizeof(pb));
  299.     pb.ATPuserData = (long) whichRequest;
  300.     pb.ATPatpSocket = socket;
  301.     pb.ATPatpFlags = atpXOvalue;
  302.     pb.ATPaddrBlock = *dest;
  303.     pb.ATPreqLength = reqSize;
  304.     pb.ATPreqPointer = (Ptr)reqBuff;
  305.     pb.ATPbdsPointer = (Ptr)&bds;
  306.     pb.ATPnumOfBuffs = numResps == 1;
  307.     pb.ATPtimeOutVal = 6;
  308.     pb.ATPretryCount = 2; // *** 
  309.  
  310.     stat = PNSendRequest(&pb, false);
  311.  
  312.     if (stat != 0)
  313.     {
  314.         sprintf((char *)respBuff, "Received error from PNSendRequest: %d", (int)stat);
  315.         return -1;
  316.     }
  317.  
  318.     return bds[0].userBytes;
  319.  }
  320.  
  321.  void LServerActions::SetConnected(Boolean connected)
  322. {
  323.     if (connected)
  324.     {
  325.         mGetName->Enable();
  326.         mCloseConn->Enable();
  327.         mCurrConn->Enable();
  328.         mPeakConn->Enable();
  329.     }
  330.     else
  331.     {
  332.         mGetName->Disable();
  333.         mCloseConn->Disable();
  334.         mCurrConn->Disable();
  335.         mPeakConn->Disable();
  336.         mResult1->SetDescriptor("\p Not Connected.");
  337.     }    
  338.     mIsConnected = connected;
  339.     Refresh();
  340. }
  341.  
  342.